package org.rsbot.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarOutputStream;

public class Extractor
  implements Runnable
{
  private final String[] args;

  private static void saveTo(InputStream paramInputStream, String paramString)
  {
    try
    {
      FileOutputStream localFileOutputStream = new FileOutputStream(new File(paramString));

      byte[] arrayOfByte = new byte[1024];
      int i;
      while ((i = paramInputStream.read(arrayOfByte)) > 0) {
        localFileOutputStream.write(arrayOfByte, 0, i);
      }
      paramInputStream.close();
      localFileOutputStream.close();
    }
    catch (Exception localException)
    {
    }
  }

  public Extractor(String[] paramArrayOfString) {
    this.args = paramArrayOfString;
  }

  public void run() {
    ClassLoader localClassLoader = getClass().getClassLoader();
    String str1 = "resources/";

    if (GlobalConfiguration.RUNNING_FROM_JAR)
    {
      try {
        if (GlobalConfiguration.getCurrentOperatingSystem() == GlobalConfiguration.OperatingSystem.WINDOWS) {
          saveTo(localClassLoader.getResourceAsStream(str1 + "Compile-Scripts.bat"), GlobalConfiguration.Paths.getHomeDirectory() + File.separator + "Compile-Scripts.bat");

          saveTo(localClassLoader.getResourceAsStream(str1 + "FindJDK.bat"), GlobalConfiguration.Paths.getHomeDirectory() + File.separator + "FindJDK.bat");
        }
        else {
          saveTo(localClassLoader.getResourceAsStream(str1 + "compile-scripts.sh"), GlobalConfiguration.Paths.getHomeDirectory() + File.separator + "compile-scripts.sh");
        }

        URL localURL = GlobalConfiguration.class.getClassLoader().getResource("resources/version.dat");
        String str2 = localURL.toString().replace("jar:file:", "").replace("!/resources/version.dat", "");
        try {
          str2 = URLDecoder.decode(str2, "UTF-8");
        } catch (UnsupportedEncodingException localUnsupportedEncodingException) {
        }
        JarFile localJarFile = new JarFile(new File(str2));
        File localFile2 = new File(GlobalConfiguration.Paths.getScriptsExtractedCache());
        FileOutputStream localFileOutputStream = new FileOutputStream(localFile2);
        JarOutputStream localJarOutputStream = new JarOutputStream(localFileOutputStream);
        Enumeration localEnumeration = localJarFile.entries();
        while (localEnumeration.hasMoreElements()) {
          JarEntry localJarEntry = (JarEntry)localEnumeration.nextElement();
          if (localJarEntry.getName().startsWith("scripts/")) {
            InputStream localInputStream = localClassLoader.getResourceAsStream(localJarEntry.getName());
            localJarOutputStream.putNextEntry(new JarEntry(localJarEntry.getName().substring(8)));
            byte[] arrayOfByte = new byte[256];
            while (true) {
              int i = localInputStream.read(arrayOfByte, 0, arrayOfByte.length);
              if (i < 0) {
                break;
              }
              localJarOutputStream.write(arrayOfByte, 0, i);
            }
            localInputStream.close();
          }
        }
        localJarOutputStream.close();
        localFileOutputStream.close();
      } catch (Exception localException) {
        localException.printStackTrace();
      }
    } else if ((this.args.length > 2) && 
      (this.args[0].toLowerCase().startsWith("delete"))) {
      File localFile1 = new File(this.args[1]);
      if (localFile1.exists())
        localFile1.delete();
    }
  }

  public void clearDirectory(File paramFile)
  {
    if (paramFile.exists())
      for (File localFile : paramFile.listFiles()) {
        if (localFile.isDirectory()) {
          clearDirectory(localFile);
        }
        if (!localFile.delete())
          System.err.println("Failed to delete file: " + localFile);
      }
  }
}